package com.offics.loan.application.service;

import com.offics.loan.application.dto.bankcard.AddBankCardRequest;
import com.offics.loan.application.dto.bankcard.BankCardResponse;
import com.offics.loan.application.dto.bankcard.UpdateBankCardRequest;
import com.offics.loan.domain.entity.BankCard;
import com.offics.loan.domain.repository.BankCardRepository;
import com.offics.loan.domain.service.BankCardDomainService;
import com.offics.loan.infrastructure.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 银行卡应用服务
 */
@Service
@RequiredArgsConstructor
public class BankCardApplicationService {

    private final BankCardRepository bankCardRepository;
    private final BankCardDomainService bankCardDomainService;

    /**
     * 添加银行卡
     *
     * @param request 添加银行卡请求
     * @return 银行卡响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AddBankCardRequest request) {
        // 创建银行卡
        BankCard bankCard = new BankCard();
        BeanUtils.copyProperties(request, bankCard);
        bankCard.setStatus(1);
        bankCard.setCreateTime(LocalDateTime.now());
        bankCard.setUpdateTime(LocalDateTime.now());

        // 添加银行卡
        return bankCardDomainService.add(bankCard);
    }

    /**
     * 更新银行卡
     *
     * @param request 更新银行卡请求
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(UpdateBankCardRequest request) {
        // 检查银行卡是否存在
        Optional<BankCard> optionalBankCard = bankCardRepository.findById(request.getId());
        if (optionalBankCard.isEmpty()) {
            throw new BusinessException("银行卡不存在");
        }

        // 创建银行卡
        BankCard bankCard = new BankCard();
        BeanUtils.copyProperties(request, bankCard);
        bankCard.setUpdateTime(LocalDateTime.now());

        // 更新银行卡
        return bankCardDomainService.update(bankCard);
    }

    /**
     * 删除银行卡
     *
     * @param id 银行卡ID
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return bankCardDomainService.delete(id);
    }

    /**
     * 设置默认银行卡
     *
     * @param id 银行卡ID
     * @param userId 用户ID
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean setDefault(Long id, Long userId) {
        return bankCardDomainService.setDefault(id, userId);
    }

    /**
     * 启用银行卡
     *
     * @param id 银行卡ID
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean enable(Long id) {
        return bankCardDomainService.enable(id);
    }

    /**
     * 禁用银行卡
     *
     * @param id 银行卡ID
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean disable(Long id) {
        return bankCardDomainService.disable(id);
    }

    /**
     * 获取银行卡
     *
     * @param id 银行卡ID
     * @return 银行卡响应
     */
    public BankCardResponse get(Long id) {
        // 获取银行卡
        Optional<BankCard> optionalBankCard = bankCardRepository.findById(id);
        if (optionalBankCard.isEmpty()) {
            throw new BusinessException("银行卡不存在");
        }

        BankCard bankCard = optionalBankCard.get();

        // 构建响应
        BankCardResponse response = new BankCardResponse();
        BeanUtils.copyProperties(bankCard, response);
        return response;
    }

    /**
     * 获取用户的银行卡列表
     *
     * @param userId 用户ID
     * @return 银行卡响应列表
     */
    public List<BankCardResponse> listByUserId(Long userId) {
        // 获取银行卡列表
        List<BankCard> bankCards = bankCardRepository.findByUserId(userId);

        // 构建响应
        return bankCards.stream().map(bankCard -> {
            BankCardResponse response = new BankCardResponse();
            BeanUtils.copyProperties(bankCard, response);
            return response;
        }).collect(Collectors.toList());
    }

    /**
     * 获取用户的默认银行卡
     *
     * @param userId 用户ID
     * @return 银行卡响应
     */
    public BankCardResponse getDefaultByUserId(Long userId) {
        // 获取默认银行卡
        Optional<BankCard> optionalBankCard = bankCardRepository.findDefaultByUserId(userId);
        if (optionalBankCard.isEmpty()) {
            throw new BusinessException("默认银行卡不存在");
        }

        BankCard bankCard = optionalBankCard.get();

        // 构建响应
        BankCardResponse response = new BankCardResponse();
        BeanUtils.copyProperties(bankCard, response);
        return response;
    }
}
